package com.person.jc.service.car;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.service.Service;
import com.person.jc.utils.Utils;

public class BdCarReturnService extends Service {
	
	public static final BdCarReturnService me = new BdCarReturnService();
	
	public List<Record> getList(Map<String,Object> map){
		String temp = "";
		List<Object> params = new ArrayList<Object>();
		StringBuffer sql= new StringBuffer("select * from bs_car_use_log where oper_user = ? and return_addr is null ");
		params.add(map.get("operUser"));
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("carCode")))){
			sql.append(" and car_no = ?");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("logId")))){
			sql.append(" and log_id = ?");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("useStatus")))){
			if("1".equals(temp)){
				sql.append(" and return_addr is null");
			}else{
				sql.append(" and return_addr is not null");
			}
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("startDate")))){
			sql.append(" and date_format(use_date, '%Y-%m-%d') >= ?");
			params.add(temp);
		}
		if(Utils.isNotEmpty(temp = String.valueOf(map.get("endtDate")))){
			sql.append(" and date_format(use_date, '%Y-%m-%d') <= ?");
			params.add(temp);
		}
		sql.append(" order by use_date desc");
		return Db.find(sql.toString(),Utils.getObjParam(params));
	}
	
	public void update(Map<String,Object> map){
		String sql = "update bs_car_use_log set return_date = ?, return_addr = ?, end_mileage = ? where log_id = ?";
		List<Object> params = new ArrayList<Object>();
		params.add(map.get("returnDate"));
		params.add(map.get("returnAddr"));
		params.add(map.get("endMileage"));
		params.add(map.get("logId"));
		Db.update(sql, Utils.getObjParam(params));
		super.setCRUD("用车申请", "U", "新增还车申请："+map.get("carNo"));
	}
}
